﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;

namespace WEB.Core.Util
{
    public class GenericToDataTable
    {
        /// <summary>
        /// Default Constructor
        /// </summary>
        private GenericToDataTable()
        { }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name=”T”>Custome Class </typeparam>
        /// <param name=”lst”>List Of The Custome Class</param>
        /// <returns> Return the class datatbl </returns>
        public static DataTable ConvertTo<T>(object item)
        {
            DataTable tbl = CreateTable<T>();
            Type entType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
            DataRow row = tbl.NewRow();
            foreach (PropertyDescriptor prop in properties)
            {
                if (prop.PropertyType.Name == "Decimal")
                {
                    row[prop.Name] = Util.FormatNumberic(prop.GetValue(item));
                }
                else if (prop.PropertyType.Name == "DateTime")
                {

                    string dt = Util.FormatDate(prop.GetValue(item));
                    if ("01/01/0001".Equals(dt))
                    {
                        row[prop.Name] = "";
                    }
                    else
                    {
                        row[prop.Name] = Util.FormatDate(prop.GetValue(item));
                    }
                }
                else
                {
                    row[prop.Name] = prop.GetValue(item);
                }
            }
            tbl.Rows.Add(row);
            return tbl;
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name=”T”>Custome Class </typeparam>
        /// <param name=”lst”>List Of The Custome Class</param>
        /// <returns> Return the class datatbl </returns>
        public static DataTable ConvertTo<T>(IList<T> lst)
        {
            //create DataTable Structure
            DataTable tbl = CreateTable<T>();
            Type entType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
            //get the list item and add into the list
            int index = 1;
            foreach (T item in lst)
            {
                DataRow row = tbl.NewRow();
                row["STT"] = index++;
                foreach (PropertyDescriptor prop in properties)
                {
                    if (prop.PropertyType.Name == "Decimal")
                    {
                        row[prop.Name] = Util.FormatNumberic(prop.GetValue(item));
                    }
                    else if (prop.PropertyType.Name == "DateTime")
                    {
                        string dt = Util.FormatDate(prop.GetValue(item));
                        if ("01/01/0001".Equals(dt))
                        {
                            row[prop.Name] = "";
                        }
                        else
                        {
                            row[prop.Name] = Util.FormatDate(prop.GetValue(item));
                        }
                    }
                    else
                    {
                        row[prop.Name] = prop.GetValue(item);
                    }
                }
                tbl.Rows.Add(row);
            }
            return tbl;
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name=”T”>Custome Class</typeparam>
        /// <returns></returns>
        public static DataTable CreateTable<T>()
        {
            //T –> ClassName
            Type entType = typeof(T);
            //set the datatable name as class name
            DataTable tbl = new DataTable(entType.Name);
            //
            tbl.Columns.Add("STT");
            //get the property list
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
            foreach (PropertyDescriptor prop in properties)
            {
                try
                {
                    Type type = prop.PropertyType;
                    if (type.Name != "Nullable`1")
                    {
                        tbl.Columns.Add(prop.Name);
                    }
                    else
                    {
                        tbl.Columns.Add(prop.Name);
                    }
                }
                catch
                {
                }
            }
            return tbl;
        }
    }
}
